package com.tenement.controller;

import com.mybatisflex.core.paginate.Page;
import com.tenement.common.Axios;
import com.tenement.enums.RespBusiness;
import com.tenement.vo.QueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.tenement.entity.Properties;
import com.tenement.service.PropertiesService;
import java.io.Serializable;

/**
 *  控制层。
 *
 * @author BertXie
 * @since 1.0.1
 */
@RestController
@RequestMapping("/properties")
@RequiredArgsConstructor
@Api(tags = "物业")
public class PropertiesController {

   private final PropertiesService propertiesService;

   @ApiOperation("操作数据")
   @PutMapping("operator")
   public Axios operator(@RequestBody Properties properties)
   {
        return propertiesService.saveOrUpdate(properties) ? Axios.success(RespBusiness.SUCCESS) : Axios.error(RespBusiness.FAIL);
   }

    @ApiOperation("根据id获取数据")
    @GetMapping("oneId/{id}")
    public Axios oneId(@PathVariable(value = "id") Serializable id)
    {
        return Axios.success(propertiesService.getById(id));
    }

   @ApiOperation("获取物业列表")
   @PreAuthorize("@exp.hasAuthority('system:property:list')")
   @PostMapping("list")
   public Axios list(@RequestBody QueryVo queryVo)
   {
       Page<Properties> page = propertiesService.list(queryVo);
       return Axios.success(page);
   }

   @ApiOperation("删除物业")
   @PreAuthorize("@exp.hasAuthority('system:property:list')")
   @DeleteMapping("delete/{id}")
   public Axios delete(@PathVariable Long id)
   {
       return propertiesService.delete(id) ? Axios.success(RespBusiness.SUCCESS) : Axios.error(RespBusiness.FAIL);
   }

   @ApiOperation("获取绑定的物业的选项")
   @GetMapping(value = {"bindSelect/{keyWord}","bindSelect"})
   public Axios bindSelect(@PathVariable(value = "keyWord",required = false) String keyWord) {
       return Axios.success(propertiesService.bindSelect(keyWord));
   }

}
